<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库名" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入仓库名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库类型" prop="storeType">
        <el-select v-model="queryParams.storeType" placeholder="请选择仓库类型" clearable>
          <el-option
            v-for="dict in dict.type.store_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="行长" prop="line">
        <el-input
          v-model="queryParams.line"
          placeholder="请输入行长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="列宽" prop="wide">
        <el-input
          v-model="queryParams.wide"
          placeholder="请输入列宽"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域数" prop="regions">
        <el-input
          v-model="queryParams.regions"
          placeholder="请输入区域数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sales:storeInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sales:storeInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sales:storeInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sales:storeInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="仓库名" align="center" prop="storeName" />
      <el-table-column label="仓库类型" align="center" prop="storeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.store_type" :value="scope.row.storeType"/>
        </template>
      </el-table-column>
      <el-table-column label="行长(米)" align="center" prop="line" />
      <el-table-column label="列宽(米)" align="center" prop="wide" />
      <el-table-column label="剩余存储数量" align="center" prop="produceCount" >
        <template slot-scope="scope">
          <span>{{ getCount(scope.row.storeId) }}</span> 
        </template>
      </el-table-column>
      <el-table-column label="区域数(块)" align="center" prop="regions" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 0 ? 'success' : 'danger'">
            <span v-if="scope.row.status == 0">仓库未满</span>
            <span v-else-if="scope.row.status == 1">仓库已满</span>
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sales:storeInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sales:storeInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库名" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入仓库名" />
        </el-form-item>
        <el-form-item label="仓库类型" prop="storeType">
          <el-select v-model="form.storeType" placeholder="请选择仓库类型">
            <el-option
              v-for="dict in dict.type.store_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行长" prop="line">
          <el-input v-model="form.line" placeholder="请输入行长" type="number"/>
        </el-form-item>
        <el-form-item label="列宽" prop="wide">
          <el-input v-model="form.wide" placeholder="请输入列宽" type="number"/>
        </el-form-item>
        <el-form-item label="区域数" prop="regions">
          <el-input v-model="form.regions" placeholder="请输入区域数" type="number"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStoreInfo, getStoreInfo, delStoreInfo, addStoreInfo, updateStoreInfo } from "@/api/sales/storeInfo";

export default {
  name: "StoreInfo",
  dicts: ['store_type'],
  data() {
    // 写长宽限制
    // 长和宽为10-100的整数
    var validateLine = (rule, value, callback) => {  
      if (value === '') {  
        callback(new Error('请输入行长'));  
      } else if (value < 10 || value > 100) {  
        callback(new Error('行长必须在10到100之间'));  
      } else {  
        callback();   
      }  
    }; 
    // 长大于等于宽
    var validateWide = (rule, value, callback) => {  
      if (value === '') {  
        callback(new Error('请输入列宽'));  
      } else if (value < 10 || value > 100) {  
        callback(new Error('列宽必须在10到100之间'));  
      } else {  
        if(this.form.line && value > this.form.line){
          callback(new Error('列宽必须小于行长'));
        } else {  
          callback();
        }  
      }  
    }; 
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库信息表格数据
      storeInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: null,
        storeType: null,
        line: null,
        wide: null,
        regions: null,
        status: null,
        trueCount: null,
        untrueCount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storeName: [
          { required: true, message: "仓库名不能为空", trigger: "blur" }
        ],
        storeType: [
          { required: true, message: "仓库类型不能为空", trigger: "change" }
        ],
        line: [
        { required: true, validator: validateLine, trigger: 'blur' }, 
        ],
        wide: [
        { required: true, validator: validateWide, trigger: 'blur' }, 
        ],
        regions: [
          { required: true, message: "区域数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getCount(storeId){
      const foundItem = this.storeInfoList.find(item => item.storeId === storeId);
      const produceCount = parseInt(foundItem.produceCount, 10);
      const result = produceCount - foundItem.trueCount - foundItem.untrueCount;
      return result;
    },
    /** 查询仓库信息列表 */
    getList() {
      this.loading = true;
      listStoreInfo(this.queryParams).then(response => {
        this.storeInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storeId: null,
        storeName: null,
        storeType: null,
        line: null,
        wide: null,
        regions: null,
        trueCount: null,
        untrueCount: null,
        status: '0',
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        del: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.storeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const storeId = row.storeId || this.ids
      getStoreInfo(storeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.storeId != null) {
            updateStoreInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStoreInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storeIds = row.storeId || this.ids;
      this.$modal.confirm('是否确认删除仓库信息编号为"' + storeIds + '"的数据项？').then(function() {
        return delStoreInfo(storeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sales/storeInfo/export', {
        ...this.queryParams
      }, `storeInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
